#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.config;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.modelmapper.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import uyun.tenant.api.entify.AppRequest;
import uyun.tenant.api.entify.PrivilegeRequest;
import uyun.tenant.serviceapi.AppServiceApi;
import uyun.tenant.serviceapi.PrivilegeServiceApi;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: yangfei
 * @desc: 注册产品在租户注册, 产品code注册
 * @date: created in 2019/1/16 11:09
 * @modifed by:
 */
@Slf4j
@Component
@EnableConfigurationProperties({AuthorityCodeProperties.class})
public class ApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent>, Ordered {

    @Autowired
    private AppProperties properties;

    @Autowired
    private AuthorityCodeProperties authorityCodeProperties;

    @Reference
    private AppServiceApi appServiceApi;

    @Reference
    private PrivilegeServiceApi privilegeServiceApi;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
        if (properties.getCode() != null) {
            AppRequest appRequest = new AppRequest();
            appRequest.setCode(properties.getCode());
            appRequest.setName(properties.getCode());
            appRequest.setDescription(properties.getName());
            appRequest.setContextPath("/" + properties.getLowerCode());
            appServiceApi.register(appRequest);
            log.debug("register product{}:" + JSON.toJSONString(appRequest));
        } else {
            log.error("register product is not null{}:" + JSON.toJSONString(properties));
        }
        if (authorityCodeProperties.getAuthCodes() != null) {
            List<PrivilegeRequest> privileges = new ArrayList<>();
            ModelMapper modelMapper = modelMapperAuthToPrivile();
            privileges = modelMapper.map(authorityCodeProperties.getAuthCodes(), new TypeToken<List<PrivilegeRequest>>() {
            }.getType());
            for (PrivilegeRequest privilegeRequest : privileges) {
                privilegeRequest.setAppCode(properties.getCode());
            }
            privilegeServiceApi.batchSave(privileges);
            log.debug("register codeInfo{}:" + JSON.toJSONString(privileges));

        } else {
            log.error("register codeInfo is not null{}:" + JSON.toJSONString(authorityCodeProperties.getAuthCodes()));
        }
    }

    private ModelMapper modelMapperAuthToPrivile() {
        ModelMapper modelMapper = new ModelMapper();
        //创建自定义映射规则
        PropertyMap<AuthorityCodeProperties.Auth, PrivilegeRequest> propertyMap = new PropertyMap<AuthorityCodeProperties.Auth, PrivilegeRequest>() {
            @Override
            protected void configure() {
                map(source.getAppCode(), destination.getAppCode());//使用自定义转换规则
                map(source.getCode(), destination.getCode());//使用自定义属性提供覆盖
                map(source.getEnName(), destination.getName());
                map(source.getName(), destination.getNameCN());
                skip(destination.getParentCode());
                skip(destination.getOrderby());
                skip(destination.getDescription());
            }
        };
        //添加映射器
        modelMapper.addMappings(propertyMap);
        modelMapper.validate();
        return modelMapper;
    }

    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE;
    }
}
